package com.yanqu.road.utils.net;

import com.yanqu.road.utils.ServerType;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 网络配置
 */
public class CrossNetConfigHelper {

    private static Logger logger = LogManager.getLogger(CrossNetConfigHelper.class.getName());

    private static Map<Integer, ServerNetConfig> webMap = new HashMap<>();
    private static Map<Integer, ServerNetConfig> crossMap = new HashMap<>();

    public static boolean init(Map<String, List<ServerNetConfig>> netConfigMap) {
        try {
            List<ServerNetConfig> webConfigList = netConfigMap.get("web_server");
            if(null != webConfigList){
                for(ServerNetConfig netConfig : webConfigList){
                    webMap.put(netConfig.getServerIndex(), netConfig);
                }
            }

            List<ServerNetConfig> crossConfigList = netConfigMap.get("cross_server");
            if(null != crossConfigList){
                for(ServerNetConfig netConfig : crossConfigList){
                    crossMap.put(netConfig.getServerIndex(), netConfig);
                }
            }
        } catch (Exception e) {
            logger.error("crossServer error", e);
            return false;
        }


        return true;
    }

    /**
     * 服务器配置
     * @param serverType
     * @param serverIndex
     * @return
     */
    public static ServerNetConfig getServerNetConfig(int serverType, int serverIndex) {
        ServerNetConfig netConfig = null;
        switch (serverType) {
            case ServerType.SERVER_WEB:
                netConfig = webMap.get(serverIndex);
                break;
            case ServerType.SERVER_CROSS:
                netConfig = crossMap.get(serverIndex);
                break;
            case ServerType.SERVER_CROSS_2:
                netConfig =crossMap.get(serverIndex);
                break;
            case ServerType.CENTER_CROSS_2:
                netConfig =crossMap.get(serverIndex);
                break;
            case ServerType.BANQUET_CROSS:
                netConfig =crossMap.get(serverIndex);
                break;
        }
        return netConfig;
    }
}
